package com.teamelite.exmgmt;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class cListAktivitasByAN  extends ListActivity {
	private ArrayList<arrList> lstActivity;
	private String id_pengguna,year,month;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.custom_clstactivity);
		DBHelper db = new DBHelper(this);
		
		this.id_pengguna = getIntent().getExtras().getString("id_pengguna");
		this.year = getIntent().getExtras().getString("year");
		this.month = getIntent().getExtras().getString("month");
		lstActivity = db.getActivityGroupAN(this.id_pengguna,this.year,this.month);
		myAdapter lstAdapter = new myAdapter(this, lstActivity, "AktivitasGroup");
		setListAdapter(lstAdapter);
		getListView().setBackgroundColor(Color.BLACK);
		registerForContextMenu(getListView());
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		DBHelper db = new DBHelper(this);
		lstActivity = db.getActivityGroupAN(this.id_pengguna,this.year,this.month);
		myAdapter lstAdapter = new myAdapter(this, lstActivity, "AktivitasGroup");
		setListAdapter(lstAdapter);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Intent listAll = new Intent(getParent(), cListAktivitas.class);
		listAll.putExtra("id_pengguna",this.id_pengguna);
		listAll.putExtra("year",this.year);
		listAll.putExtra("id_activity", lstActivity.get(position).id_activity);
		listAll.putExtra("month",lstActivity.get(position).month_activitydo);
		TabGroupActivity parentActivity = (TabGroupActivity)getParent();
 		parentActivity.startChildActivity("cListAktivitas", listAll);
 		
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
	    getMenuInflater().inflate(R.menu.activitylist_menu, menu);
	}
	public AlertDialog myAlertDialog;
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
	            .getMenuInfo();
		switch (item.getItemId()) {
	    	case R.id.mRemove:
	    		if( myAlertDialog != null && myAlertDialog.isShowing() ) return true;
	            AlertDialog.Builder builder = new AlertDialog.Builder(getParent());
	            builder.setTitle("Are You Sure To Remove This Data?");
	            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// Do Nothing
					}
				});
	            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
	            	@Override
	                public void onClick(DialogInterface dialog, int arg1) {
	                  	DBHelper db = new DBHelper(cListAktivitasByAN.this);
	                  	db.delActivityGroupAN(cListAktivitasByAN.this.id_pengguna, lstActivity.get(info.position).id_activity);
	                  	lstActivity = db.getActivityGroupAN(cListAktivitasByAN.this.id_pengguna,cListAktivitasByAN.this.year,cListAktivitasByAN.this.month);
	            		myAdapter lstAdapter = new myAdapter(cListAktivitasByAN.this, lstActivity, "AktivitasGroup");
	            		setListAdapter(lstAdapter);
	            	}
	            });
	            builder.setCancelable(false);
	            myAlertDialog = builder.create();
	            myAlertDialog.show();
	        return true;
	    	case R.id.mCancel:
	    	
	    	return true;
	    	default:
	    		
	    	return true;
	    }
	}

}
